<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

Class Pon_model extends CI_Model {

    private $table = "pon";
    private $stat_tb = 'pon_equip';

    function __construct() {
        parent::__construct();
    }

    function insert_data($data = array()) {
        if ($data != '') {
            $val = array(
                'pon_code' => $data['pon_code'],
                'pon_name' => $data['pon_name'],
                'pon_odf' => $data['pon_odf'],
                'pon_olt' => $data['pon_olt'],
                'pon_shelf' => $data['pon_shelf'],
                'pon_slot' => $data['pon_slot'],
                'pon_addr' => $data['pon_addr'],
                'pon_vlan_id' => $data['pon_vlan_id'],
                'pon_addr' => $data['pon_addr'],
                'pon_commend' => $data['pon_commend'],
                'pon_latitude' => $data['pon_latitude'],
                'pon_longtitude' => $data['pon_longtitude']
            );
            $stat_val = array(
                'pon_eq_stat_id' => '0'
            );
            $rs = $this->select_by_filter($this->table, $val['pon_id']);
            $is = (!empty($rs) && $rs > 0) ? FALSE : ($this->db->insert($this->table, $val) ? $this->db->insert_id() : FALSE);
            if ($is) {
                $this->set_up($val['pon_code']);
            }
            return $is;
        }
    }

    function set_up($param) {
        if ($param != '') {
            $query = $this->db->where('pon_code', $param)->get($this->table)->result();
            foreach ($query as $rs) {
                $val = $rs->pon_id;
            }
            $stat = array(
                'pon_id' => $val,
                'pon_eq_stat_id' => '0'
            );
            $str = ($val) ? ($this->db->insert($this->stat_tb, $stat) ? TRUE : FALSE) : FALSE;

            return $str;
        }
    }

    function check_insert($table, $val) {
        $rs = $this->db->where('pon_id', $val)->get($table);
        $count = $rs->num_rows();
        return $count;
    }

    function select_by_table($table) {
        $query = $this->db->get($table)->result();
        return $query;
    }

    function select_by_filter($id) {
        $rs = $this->db->where('pon_id', $id)->get($this->table)->row_array();
        return $rs;
    }

    function update_data($data = array()) {
        if ($data != '') {
            $val = array(
                'pon_code' => $data['pon_code'],
                'pon_name' => $data['pon_name'],
                'pon_odf' => $data['pon_odf'],
                'pon_olt' => $data['pon_olt'],
                'pon_shelf' => $data['pon_shelf'],
                'pon_slot' => $data['pon_slot'],
                'pon_addr' => $data['pon_addr'],
                'pon_vlan_id' => $data['pon_vlan_id'],
                'pon_addr' => $data['pon_addr'],
                'pon_commend' => $data['pon_commend'],
                'pon_latitude' => $data['pon_latitude'],
                'pon_longtitude' => $data['pon_longtitude']
            );
            $is = ($this->db->where('pon_id', $data['pon_id'])->update($this->table, $val)) ? TRUE : FALSE;
            return $is;
        }
    }

    function delete_data($data) {
        ($this->db->where('pon_id', $data)->delete($this->table)) ? $is = TRUE : $is = FALSE;
        return $is;
    }

}
